#include "Window.h"

namespace Cartesia {

int Window::_width = 0;
int Window::_height = 0;
HWND Window::_hWnd;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) {
		case WM_PAINT:
			//SwapBuffers();
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}


ATOM Window::Register(HINSTANCE hInstance)
{
	WNDCLASS wc;

	memset(static_cast<void *> (&wc), 0, sizeof(WNDCLASS));

	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = (WNDPROC) WndProc;
	wc.hInstance = hInstance;
	wc.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
	wc.lpszClassName = "ClassName";

	ATOM atom = RegisterClass(&wc);

	return atom;
}


HWND Window::Create(const char* title, int width, int height, HINSTANCE hInstance)
{
	Window::Register(hInstance);

	_width = width;
	_height = height;

	RECT rect;

	rect.top = 0;
	rect.left = 0;
	rect.right = _width;
	rect.bottom = _height;

	AdjustWindowRect(&rect,WS_OVERLAPPEDWINDOW-WS_THICKFRAME, false);

	_hWnd = CreateWindow("ClassName",
						title,
						WS_OVERLAPPEDWINDOW-WS_THICKFRAME,
						0,//1152/2 - _width/2,
						0,//864/2 - _height/2,
						rect.right - rect.left,
						rect.bottom - rect.top,
						NULL,
						NULL,
						hInstance,
						NULL
	);


	if (!_hWnd) {
		return NULL;
	}

	ShowWindow(_hWnd,SW_SHOW);
	UpdateWindow(_hWnd);

	return _hWnd;

}


} // End namespace